//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.Helpers
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.ComponentModel;
    using System.Globalization;
    using Microsoft.Research.ScientificWorkflow.NetCDF.CSharpAPI;
    using Microsoft.Research.ScientificWorkflow.Helpers;
    

    #endregion

    #region Variable Class

    [Serializable]
    [TypeConverter(typeof(VariableConverter))]
	public class Variable
    {
        #region Private Fields

        private string variableName;
        private string variableType;
        private const string VariableTypePropertyName = "VariableType";

        #endregion

        #region Public Properties

        public string VariableName
        {
            get { return this.variableName; }
            set { this.variableName = value; }
        }

        public string VariableType
        {
            get { return this.variableType; }
            set 
            { 
                this.variableType = value; 
            }
        }
        
        [Browsable(false)]
        public Type ClrType
        {
            get 
            {
                NetCDFDataType varType =
					(NetCDFDataType)Enum.Parse(typeof(NetCDFDataType), variableType);

                return TypeMapper.GetClrType(varType);
            }
        }

        #endregion

        #region Overriden Methods

        public override string ToString()
        {
            string str = base.ToString();
            if (!string.IsNullOrEmpty(variableName) &&
                !string.IsNullOrEmpty(variableType))
            {
                str = string.Format(
                    CultureInfo.CurrentUICulture, 
                    "'{0}' -> [{1}]", 
                    new object[] { variableName, variableType.ToString() });
            }
            
            return str;
        }

        #endregion
    }

    #endregion

    public class VariableConverter : TypeConverter
    {
        // Overrides the CanConvertFrom method of TypeConverter.
        // The ITypeDescriptorContext interface provides the context for the
        // conversion. Typically, this interface is used at design time to 
        // provide information about the design-time container.
        public override bool CanConvertFrom(ITypeDescriptorContext context,
           Type sourceType)
        {

            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        // Overrides the ConvertFrom method of TypeConverter.
        public override object ConvertFrom(ITypeDescriptorContext context,
           CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] v = ((string)value).Split(new char[] { ',' });
                if (v.Length % 2 != 0)
                    throw new ArgumentException("Error Converting from String to Variable Array");

                Variable[] varArray = new Variable[v.Length / 2];
                for (int i = 0, j = 0; i < v.Length; j++)
                {
                    Variable var = new Variable();

                    var.VariableName = v[i];
                    var.VariableName = v[i + 1];
                    varArray[j] = var;
                    i = i + 2;
                }
                return varArray;
            }
            return base.ConvertFrom(context, culture, value);
        }
        //// Overrides the ConvertTo method of TypeConverter.
        //public override object ConvertTo(ITypeDescriptorContext context,
        //   CultureInfo culture, object value, Type destinationType)
        //{
        //    if (destinationType == typeof(string))
        //    {
        //        return ((Point)value).X + "," + ((Point)value).Y;
        //    }
        //    return base.ConvertTo(context, culture, value, destinationType);
        //}
    }
}
